package com.imay.live.manager;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.imay.live.IMApplication;
import com.imay.live.model.MusicModel;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/25.
 */

public class LocalMusicManager {
   public static String  musicDownLoadPath = IMApplication.getInstance().getExternalFilesDir(Environment.DIRECTORY_MUSIC).getPath();
    private static class LocalMusicManagerHolder {
        private static final LocalMusicManager INSTANCE = new LocalMusicManager();
    }

    public static final LocalMusicManager getInstance() {
        return LocalMusicManagerHolder.INSTANCE;
    }

    private LocalMusicManager() {

    }

    public File[] getLocalMusicList() {
        File file = new File(musicDownLoadPath);
        if (file.isDirectory()) {
            File[] fileArray = file.listFiles();
            return fileArray;
        }
        return null;
    }

    public List<MusicModel> checkMusicExist( List<MusicModel> musicModels) {
        File[] localMusicArr = getLocalMusicList();
        List<File> oldMusic = new ArrayList<>();
        boolean hasSame = false;
        if (localMusicArr!=null) {
            for (int i = 0; i <localMusicArr.length; i++) {
                hasSame = false;
                for (MusicModel model:musicModels) {
                    if (localMusicArr[i].getName().equals(model.getMucicName())){
                        model.setStatu(MusicModel.STATU_DOWNLOADED);
                        hasSame = true;
                        break;
                    }
                }
                if (!hasSame){
                    oldMusic.add(localMusicArr[i]);
                }
            }
        }
        for (File file:oldMusic) {
            file.delete();
        }
        return musicModels;
    }

    /**
     *
     * @param musicName id + updatetime 拼成的name
     * @return
     */
    public boolean checkMusicExist(String musicName){
        File[] localMusicArr = getLocalMusicList();
        for (File music:localMusicArr) {
            if (music.getName().equals(musicName)){
                return true;
            }
        }
        return false;

    }




}
